package com.sunjob.loginserver.controller;

import com.sunjob.common.domain.img.SysImg;
import com.sunjob.common.result.Result;
import com.sunjob.loginserver.otherService.ImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Wangzurong
 * @create 2022-08-09 10:29
 */
@RestController
public class TestController {
    @Autowired
    ImgService imgService;
    @RequestMapping("/pushImg")
    public Result test(@RequestPart("img")MultipartFile multipartFile) throws IOException {
        byte[] bytes = multipartFile.getBytes();
        SysImg sysImg = new SysImg();
        sysImg.setData(bytes);
        String fileName = multipartFile.getOriginalFilename();
        // 获取后缀名
        String[] split = fileName.split("\\.");
        try {
            String suff = split[split.length-1];
            sysImg.setImgUrl("loginServer/"+UUID.randomUUID()+"."+suff);
        }catch (Exception e){
            return Result.fail("文件名含有非法字符");
        }
        return imgService.pushImg(sysImg);
    }
    @RequestMapping("/pushImgPrivate")
    public Result test1(@RequestPart("img")MultipartFile multipartFile) throws IOException {
        byte[] bytes = multipartFile.getBytes();
        SysImg sysImg = new SysImg();
        sysImg.setData(bytes);
        String fileName = multipartFile.getOriginalFilename();
        // 获取后缀名
        String[] split = fileName.split("\\.");
        try {
            String suff = split[split.length-1];
            sysImg.setImgUrl(UUID.randomUUID()+"."+suff);
        }catch (Exception e){
            return Result.fail("文件名含有非法字符");
        }
        return imgService.pushImgPrivate(sysImg);
    }
}
